import { createRouter, createWebHashHistory } from 'vue-router'
import type { RouteRecordRaw } from 'vue-router'
import { getToken } from '@/utils/auth'
const staticRoutes: RouteRecordRaw[] = [
  {
    path: '/:pathMatch(.*)*',
    component: () => import('@/views/error/404.vue'),
    meta: { hidden: true, title: '404' },
  },
  {
    path: '/login',
    component: () => import('@/views/login.vue'),
    meta: { hidden: true, title: 'Login' },
  },
  {
    path: '/',
    redirect: '/search',
  },
  {
    path: '/search',
    component: () => import('@/views/search/search.vue'),
    meta: { hidden: true, title: 'Search' },
  },
]

const router = createRouter({
  history: createWebHashHistory(),
  routes: staticRoutes,
})
router.beforeEach((to, _from, next) => {
  const token = getToken()
  if (!token && to.path !== '/login') {
    next('/login')
    return
  }
  next()
})

export default router
